package com.example.myapp.Activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapp.ADO.FriendAapter;
import com.example.myapp.Loading.ShapeLoadingDialog;
import com.example.myapp.Model.Fensi;
import com.example.myapp.Model.User;
import com.example.myapp.R;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class Myfensi extends AppCompatActivity {
    private ListView lsitnew;
    private ShapeLoadingDialog shapeLoadingDialog;
    private User user = BmobUser.getCurrentUser(User.class);//查自己
   private List<User> userList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);  //隐藏标题栏
        setContentView(R.layout.myguanzhu);
        lsitnew=findViewById(R.id.myguanzhulistview);
        ImageView myguanzhu_back=findViewById(R.id.myguanzhu_back);
        TextView myguanzhu_title=findViewById(R.id.myguanzhu_title);
        myguanzhu_title.setText("我的粉丝");
        myguanzhu_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        queryALLFensi();
    }
    @Override
    public void onBackPressed(){
        finish();
    }
    /**
     查询一对多，我的所有粉丝
     */
    private void queryALLFensi(){
        openLoading();
        BmobQuery<Fensi> query = new BmobQuery<>();
        query.addWhereEqualTo("user", BmobUser.getCurrentUser(User.class).getObjectId());
        query.include("fensi,user");
        query.findObjects(new FindListener<Fensi>() {
            @Override
            public void done(List<Fensi> object, BmobException e) {
                if (e == null) {
                    Log.e("kkkk","粉丝个数"+object.size());
                    if (object.size()>0){
                        Log.e("kkkk","粉丝0"+object.get(0).getFensi().getObjectId());
                        for (int i=0;i<object.size();i++){
                            userList.add(object.get(i).getFensi());
                        }
                      FriendAapter adapter = new FriendAapter(Myfensi.this,userList,"yes");//NewAdapterDAO为自定义适配器
                        lsitnew.setAdapter(adapter);
                    }

                } else {
                    Log.e("kkkk","粉丝失败"+ e.getMessage());
                }
                closeLoading();
            }
        });
    }
    //网络加载
    private  void openLoading(){
        shapeLoadingDialog = new ShapeLoadingDialog.Builder(Myfensi.this)
                .loadText("加载中...")
                .build();
        shapeLoadingDialog.show();
    }
    //关闭网络加载
    private  void closeLoading(){
        shapeLoadingDialog.dismiss();
    }
}
